/**
  ******************************************************************************
  * @file    EXTI/EXTI_Config/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "nixie_tube.h"
#include "protocol.h"
#include "aip1628.h"
#include "uart_com.h"
extern void led_show(void);
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup EXTI_Config
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/*
void USART1_IRQHandler(void)
{
	u8 res;
	//uint8_t data;
	 if(USART_GetITStatus(USART1,USART_IT_RXNE))
 {
     res= USART_ReceiveData(USART1); 
	   nixie_tube_show_seven(1);
     USART_SendData(USART1,res);
  //   UART_PutStr(USART1, "hello world!");             //  字符串  
  }
}
*/
int irq_cnt = 0;
unsigned char data_length = 0;
unsigned char key_flag = 0;
unsigned char timer_resend_cnt = 0;
extern unsigned char recvdata[20];
extern uint8      g_pc_order_done;
extern void TIM3_Int_Init(u16 arr,u16 psc);
extern void TIM3_Int_DeInit();
void USART1_IRQHandler(void)
{
    u8 res;	    
		
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 接收到数据
    {	 	
			  USART_ClearITPendingBit(USART1,USART_IT_RXNE);
        res = USART_ReceiveData(USART1); // 读取接收到的数据USART1->DR
			uart_send_data(USART1, res);
        if(data_length < 5)
        {
					//nixie_tube_show_one(1);
            recvdata[data_length] = res; // 记录接收到的值
            data_length++;
        } else {
					//nixie_tube_show_seven(1);
				    data_length = 0;
				}
				 
    }

}

void USART2_IRQHandler(void)
{
    u8 res;	    
				
    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {	 // 接收到数据
			  USART_ClearITPendingBit(USART2,USART_IT_RXNE);
			  queue_add_msg(&g_byteRxComBuf,USART_ReceiveData(USART2));
				//uart_send_data(USART1, USART_ReceiveData(USART2));
			 // DecMsg(&g_byteRxComBuf , MSGSIZE);
    }
}
/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}

void TIM3_IRQHandler(void)   //TIM3中断
{//检查指定的TIM中断发生与否:TIM 中断源 
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
    			//uart_send_data(USART1, 0x79);
			  if (g_pc_order_done == 1) {
						UART_485_TX_CTRL;
				    send_master_procotol(REPLY_CMD_CLEAR_SHOW,0);
				    UART_485_RX_CTRL;
				}
	//			show_num(222+timer_resend_cnt);
				timer_resend_cnt++;
				if(timer_resend_cnt > 3){
						TIM3_Int_DeInit();  //resend 3 time close TIM3		
				}
				
    }
}
/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles External line 0 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI0_IRQHandler(void)
{
	//led_show();
			
  if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  {
    /* Clear the  EXTI line 0 pending bit */
    //EXTI_ClearITPendingBit(EXTI_Line0);
		//nixie_tube_show_five(1);
  }
}
	int a=0;
/**
  * @brief  This function handles External lines 9 to 5 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI9_5_IRQHandler(void)
{
#if defined (STM32F10X_HD_VL) || defined (STM32F10X_HD) || defined (STM32F10X_XL)
  if(EXTI_GetITStatus(EXTI_Line8) != RESET)
  {
    /* Toggle LED2 */
     STM_EVAL_LEDToggle(LED2);

    /* Clear the  EXTI line 8 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line8);
  }
#else
  if(EXTI_GetITStatus(EXTI_Line5) != RESET)
  {
    /* Toggle LED2 */
   //  STM_EVAL_LEDToggle(LED2);
   // led_show();
	//	a ^= 0x1;
	//	GPIO_WriteBit(GPIOA, GPIO_Pin_11,a);
		uart_send_data(USART1, 0x56);
		if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == Bit_RESET) {
			medey(5);
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == Bit_RESET) {
				irq_cnt++;
				if (key_flag == 0 ) {//|| key_flag == 2
				Tm1628_ClearDisplay();
				if (key_flag == 0) {
				    UART_485_TX_CTRL;
				    send_master_procotol(REPLY_CMD_CLEAR_SHOW,g_show_number);
				    UART_485_RX_CTRL;
						TIM3_Int_Init(19990,7199); //5s
					  g_pc_order_done = 1;
				}
				key_flag = 1;
				} else if (key_flag == 1){
				 // key_flag = 2;
					Tm1628_ClearDisplay();
					if(g_show_number != 0) {
						//show_num(g_show_number);//while query show
					} else {
						//show_line_noorder();
					}
				}
			}
		}
    /* Clear the  EXTI line 9 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line5);
  } else if(EXTI_GetITStatus(EXTI_Line6) != RESET) {
			//			uart_send_data(USART1, 0x78);

    /* Clear the  EXTI line 6 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line6);
		medey(5);
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == Bit_RESET) {
		irq_cnt++;
				if (key_flag == 0 ) {//|| key_flag == 2
				Tm1628_ClearDisplay();
				if (key_flag == 0) {
				    UART_485_TX_CTRL;
				    send_master_procotol(REPLY_CMD_CLEAR_SHOW,0);
				    UART_485_RX_CTRL;
						TIM3_Int_Init(19990,7199); //5s
					  g_pc_order_done = 1;
				}
				key_flag = 1;
				} else if (key_flag == 1){
				 // key_flag = 2;
					Tm1628_ClearDisplay();
					if(g_show_number != 0) {
						//show_num(g_show_number);//while query show
					} else {
						//show_line_noorder();
					}
				}
			}
	}
#endif
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
